package com.kg.rails.component.editor.view;

import com.kg.rails.component.editor.model.Cell;
import com.kg.rails.component.editor.util.EditorRectangle;

import java.util.Hashtable;

public class TemporaryCellStates {
    /**
     *
     */
    protected GraphView view;

    /**
     *
     */
    protected Hashtable<Object, CellState> oldStates;

    /**
     *
     */
    protected EditorRectangle oldBounds;

    /**
     *
     */
    protected double oldScale;

    /**
     * Constructs a new temporary cell states instance.
     */
    public TemporaryCellStates(GraphView view) {
        this(view, 1, null);
    }

    /**
     * Constructs a new temporary cell states instance.
     */
    public TemporaryCellStates(GraphView view, double scale) {
        this(view, scale, null);
    }

    /**
     * Constructs a new temporary cell states instance.
     */
    public TemporaryCellStates(GraphView view, double scale, Object[] cells) {
        this.view = view;

        // Stores the previous state
        oldBounds = view.getGraphBounds();
        oldStates = view.getStates();
        oldScale = view.getScale();

        // Creates space for the new states
        view.setStates(new Hashtable<Object, CellState>());
        view.setScale(scale);

        if (cells != null) {
            // Creates virtual parent state for validation
            CellState state = view.createState(new Cell());

            // Validates the vertices and edges without adding them to
            // the model so that the original cells are not modified
            for (int i = 0; i < cells.length; i++) {
                view.validateBounds(state, cells[i]);
            }

            EditorRectangle bbox = null;

            for (int i = 0; i < cells.length; i++) {
                EditorRectangle bounds = view.validatePoints(state, cells[i]);

                if (bounds != null) {
                    if (bbox == null) {
                        bbox = bounds;
                    } else {
                        bbox.add(bounds);
                    }
                }
            }

            if (bbox == null) {
                bbox = new EditorRectangle();
            }

            view.setGraphBounds(bbox);
        }
    }

    /**
     * Destroys the cell states and restores the state of the graph view.
     */
    public void destroy() {
        view.setScale(oldScale);
        view.setStates(oldStates);
        view.setGraphBounds(oldBounds);
    }

}
